package com.jzo2o.health.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.health.model.domain.ReservationSetting;
import com.jzo2o.health.model.excel.ReservationImportData;
import com.jzo2o.health.service.IReservationSettingService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

@Component
@RequiredArgsConstructor
public class ExcelListener extends AnalysisEventListener<ReservationImportData> {
    private final IReservationSettingService reservationSettingService;


    ReservationSetting reservationSetting = new ReservationSetting();

    //调用处理exel的数据
    @Override
    public void invoke(ReservationImportData data, AnalysisContext analysisContext) {
        LocalDate date = LocalDate.parse(data.getDate());
        reservationSetting.setOrderDate(date);
        reservationSetting.setNumber(data.getNumber());
        ReservationSetting one = reservationSettingService.lambdaQuery().eq(ReservationSetting::getOrderDate, date).one();
        boolean saveOrupdate = true;
        if (one != null) {
            reservationSetting.setId(one.getId());
            saveOrupdate = reservationSettingService.updateById(reservationSetting);
        } else {
            saveOrupdate = reservationSettingService.save(reservationSetting);
        }
        if (!saveOrupdate) {
            throw new DBException("设置失败");
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}